<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="utf-8">
    <meta name="viewport" content="width=device-width, initial-scale=1">
    <meta name="keywords" content="Hexo Theme Redefine">
    
    <meta name="author" content="ChenGreen">
    <!-- preconnect -->
    <link rel="preconnect" href="https://fonts.googleapis.com">
    <link rel="preconnect" href="https://fonts.gstatic.com" crossorigin>
    
    
    <!--- Seo Part-->
    
    <link rel="canonical" href="https://chengreen.gitee.io/blog/2023/08/30/linux驱动函数介绍/"/>
    <meta name="robots" content="index,follow">
    <meta name="googlebot" content="index,follow">
    <meta name="revisit-after" content="1 days">
    
        <meta name="description" content="注册函数注册设备号123static inline int register_chrdev(unsigned int major, const char *name, const struct file_operations *fops)&amp;#123;return __register_chrdev(major, 0, 256, name, fops);&amp;#125;   输入参数 marjor：指定">
<meta property="og:type" content="article">
<meta property="og:title" content="Linux驱动函数介绍">
<meta property="og:url" content="https://chengreen.gitee.io/blog/2023/08/30/Linux%E9%A9%B1%E5%8A%A8%E5%87%BD%E6%95%B0%E4%BB%8B%E7%BB%8D/index.html">
<meta property="og:site_name" content="Hexo">
<meta property="og:description" content="注册函数注册设备号123static inline int register_chrdev(unsigned int major, const char *name, const struct file_operations *fops)&amp;#123;return __register_chrdev(major, 0, 256, name, fops);&amp;#125;   输入参数 marjor：指定">
<meta property="og:locale" content="en_US">
<meta property="article:published_time" content="2023-08-30T13:29:09.000Z">
<meta property="article:modified_time" content="2023-08-30T14:00:41.493Z">
<meta property="article:author" content="John Doe">
<meta name="twitter:card" content="summary">
    
    
    <!--- Icon Part-->
    <link rel="icon" type="image/png" href="/blog/images/redefine-favicon.svg" sizes="192x192">
    <link rel="apple-touch-icon" sizes="180x180" href="/blog/images/redefine-favicon.svg">
    <meta name="theme-color" content="#A31F34">
    <link rel="shortcut icon" href="/blog/images/redefine-favicon.svg">
    <!--- Page Info-->
    
    <title>
        
            Linux驱动函数介绍 -
        
        ChenGreen&#39;s Home
    </title>
    
<link rel="stylesheet" href="/blog/css/style.css">

    
<link rel="stylesheet" href="/blog/fonts/fonts.css">

    
<link rel="stylesheet" href="/blog/fonts/Satoshi/satoshi.css">

    
<link rel="stylesheet" href="/blog/fonts/Chillax/chillax.css">

    <!--- Font Part-->
    
    
    
    

    <!--- Inject Part-->
    
    <script id="hexo-configurations">
    let Global = window.Global || {};
    Global.hexo_config = {"hostname":"chengreen.gitee.io","root":"/blog/","language":"en","path":"search.xml"};
    Global.theme_config = {"articles":{"style":{"font_size":"16px","line_height":1.5,"image_border_radius":"14px","image_alignment":"center","image_caption":false,"link_icon":true},"word_count":{"enable":true,"count":true,"min2read":true},"author_label":{"enable":true,"auto":false,"list":[]},"code_block":{"copy":true,"style":"mac","font":{"enable":false,"family":null,"url":null}},"toc":{"enable":true,"max_depth":3,"number":false,"expand":true,"init_open":true},"copyright":false,"lazyload":true,"recommendation":{"enable":false,"title":"推荐阅读","limit":3,"mobile_limit":2,"placeholder":"/images/wallhaven-wqery6-light.webp","skip_dirs":[]}},"colors":{"primary":"#A31F34","secondary":null},"global":{"fonts":{"chinese":{"enable":false,"family":null,"url":null},"english":{"enable":false,"family":null,"url":null}},"content_max_width":"1000px","sidebar_width":"210px","hover":{"shadow":true,"scale":false},"scroll_progress":{"bar":false,"percentage":true},"website_counter":{"url":"https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js","enable":true,"site_pv":true,"site_uv":true,"post_pv":true},"single_page":true,"open_graph":true,"google_analytics":{"enable":false,"id":null}},"home_banner":{"enable":true,"style":"fixed","image":{"light":"/images/wallhaven-wqery6-light.webp","dark":"/images/wallhaven-wqery6-dark.webp"},"title":"ChenGreen's Home","subtitle":{"text":["Loading..."],"hitokoto":{"enable":true,"api":"https://v1.hitokoto.cn"},"typing_speed":100,"backing_speed":80,"starting_delay":500,"backing_delay":1500,"loop":true,"smart_backspace":true},"text_color":{"light":"#fff","dark":"#d1d1b6"},"text_style":{"title_size":"2.8rem","subtitle_size":"1.5rem","line_height":1.2},"custom_font":{"enable":false,"family":null,"url":null},"social_links":{"enable":true,"links":null,"qrs":{"weixin":null}}},"plugins":{"feed":{"enable":false},"aplayer":{"enable":false,"type":"fixed","audios":[{"name":null,"artist":null,"url":null,"cover":null}]},"mermaid":{"enable":false,"version":"9.3.0"}},"version":"2.3.0","navbar":{"auto_hide":false,"color":{"left":"#f78736","right":"#367df7","transparency":35},"links":{"Home":{"path":"/","icon":"fa-regular fa-house"},"Archives":{"path":"/archives","icon":"fa-regular fa-archive"},"Tags":{"path":"/tags","icon":"fa-regular fa-tag"},"Categories":{"path":"/categories","icon":"fa-regular fa-folder"}},"search":{"enable":false,"preload":true}},"page_templates":{"friends_column":2,"tags_style":"blur"},"home":{"sidebar":{"enable":true,"position":"left","first_item":"info","announcement":"Love Yourself","links":{"Archives":{"path":"/archives","icon":"fa-regular fa-archive"},"Tags":{"path":"/tags","icon":"fa-regular fa-tags"},"Categories":{"path":"/categories","icon":"fa-regular fa-folder"}}},"article_date_format":"auto","categories":{"enable":true,"limit":3},"tags":{"enable":true,"limit":3}},"footerStart":"2022/8/17 11:45:14"};
    Global.language_ago = {"second":"%s seconds ago","minute":"%s minutes ago","hour":"%s hours ago","day":"%s days ago","week":"%s weeks ago","month":"%s months ago","year":"%s years ago"};
    Global.data_config = {"masonry":false};
  </script>
    
    <!--- Fontawesome Part-->
    
<link rel="stylesheet" href="/blog/fontawesome/fontawesome.min.css">

    
<link rel="stylesheet" href="/blog/fontawesome/brands.min.css">

    
<link rel="stylesheet" href="/blog/fontawesome/solid.min.css">

    
<link rel="stylesheet" href="/blog/fontawesome/regular.min.css">

    
    
    
    
<meta name="generator" content="Hexo 6.3.0"></head>


<body>
<div class="progress-bar-container">
    

    
        <span class="pjax-progress-bar"></span>
        <span class="swup-progress-icon">
            <i class="fa-solid fa-circle-notch fa-spin"></i>
        </span>
    
</div>


<main class="page-container" id="swup">
    

    

    <div class="main-content-container">
        

        <div class="main-content-header">
            <header class="navbar-container">
    
    <div class="navbar-content">
        <div class="left">
            
            <a class="logo-title" href="/">
                
                ChenGreen&#39;s Home
                
            </a>
        </div>

        <div class="right">
            <!-- PC -->
            <div class="desktop">
                <ul class="navbar-list">
                    
                        
                            <li class="navbar-item">
                                <!-- Menu -->
                                <a class="" 
                                    href="/blog/"  >
                                    
                                        
                                            <i class="fa-regular fa-house"></i>
                                        
                                        HOME
                                    
                                </a>
                                <!-- Submenu -->
                                
                            </li>
                    
                        
                            <li class="navbar-item">
                                <!-- Menu -->
                                <a class="" 
                                    href="/blog/archives"  >
                                    
                                        
                                            <i class="fa-regular fa-archive"></i>
                                        
                                        ARCHIVES
                                    
                                </a>
                                <!-- Submenu -->
                                
                            </li>
                    
                        
                            <li class="navbar-item">
                                <!-- Menu -->
                                <a class="" 
                                    href="/blog/tags"  >
                                    
                                        
                                            <i class="fa-regular fa-tag"></i>
                                        
                                        TAGS
                                    
                                </a>
                                <!-- Submenu -->
                                
                            </li>
                    
                        
                            <li class="navbar-item">
                                <!-- Menu -->
                                <a class="" 
                                    href="/blog/categories"  >
                                    
                                        
                                            <i class="fa-regular fa-folder"></i>
                                        
                                        CATEGORIES
                                    
                                </a>
                                <!-- Submenu -->
                                
                            </li>
                    
                    
                </ul>
            </div>
            <!-- Mobile -->
            <div class="mobile">
                
                <div class="icon-item navbar-bar">
                    <div class="navbar-bar-middle"></div>
                </div>
            </div>
        </div>
    </div>

    <!-- Mobile drawer -->
    <div class="navbar-drawer">
        <ul class="drawer-navbar-list">
            
                
                    <li class="drawer-navbar-item flex-center">
                        <a class="" 
                        href="/blog/"  >
                             
                                
                                    <i class="fa-regular fa-house"></i>
                                
                                HOME
                            
                        </a>
                    </li>
                    <!-- Submenu -->
                    
            
                
                    <li class="drawer-navbar-item flex-center">
                        <a class="" 
                        href="/blog/archives"  >
                             
                                
                                    <i class="fa-regular fa-archive"></i>
                                
                                ARCHIVES
                            
                        </a>
                    </li>
                    <!-- Submenu -->
                    
            
                
                    <li class="drawer-navbar-item flex-center">
                        <a class="" 
                        href="/blog/tags"  >
                             
                                
                                    <i class="fa-regular fa-tag"></i>
                                
                                TAGS
                            
                        </a>
                    </li>
                    <!-- Submenu -->
                    
            
                
                    <li class="drawer-navbar-item flex-center">
                        <a class="" 
                        href="/blog/categories"  >
                             
                                
                                    <i class="fa-regular fa-folder"></i>
                                
                                CATEGORIES
                            
                        </a>
                    </li>
                    <!-- Submenu -->
                    
            

        </ul>
    </div>

    <div class="window-mask"></div>

</header>


        </div>

        <div class="main-content-body">

            

            <div class="main-content">

                
                    <div class="post-page-container">
    <div class="article-content-container">

        <div class="article-title">
            
                <h1 class="article-title-regular">Linux驱动函数介绍</h1>
            
            </div>
            
                    
        
        
            <div class="article-header">
                <div class="avatar">
                    <img src="/blog/images/maomao.png">
                </div>
                <div class="info">
                    <div class="author">
                        <span class="name">ChenGreen</span>
                        
                            <span class="author-label">Lv3</span>
                        
                    </div>
                    <div class="meta-info">
                        <div class="article-meta-info">
    <span class="article-date article-meta-item">
        <i class="fa-regular fa-pen-fancy"></i>&nbsp;
        <span class="desktop">2023-08-30 21:29:09</span>
        <span class="mobile">2023-08-30 21:29:09</span>
        <span class="hover-info">Created</span>
    </span>
    
        <span class="article-date article-meta-item">
            <i class="fa-regular fa-wrench"></i>&nbsp;
            <span class="desktop">2023-08-30 22:41</span>
            <span class="mobile">2023-08-30 22:41</span>
            <span class="hover-info">Updated</span>
        </span>
    

    
    

    
    
    
    
        <span class="article-pv article-meta-item">
            <i class="fa-regular fa-eye"></i>&nbsp;<span id="busuanzi_value_page_pv"></span>
        </span>
    
</div>

                    </div>
                </div>
            </div>
        

        <div class="article-content markdown-body">
            <h1 id="注册函数"><a href="#注册函数" class="headerlink" title="注册函数"></a>注册函数</h1><h4 id="注册设备号"><a href="#注册设备号" class="headerlink" title="注册设备号"></a>注册设备号</h4><div class="highlight-container" data-rel="C"><figure class="iseeu highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> <span class="keyword">inline</span> <span class="type">int</span> <span class="title function_">register_chrdev</span><span class="params">(<span class="type">unsigned</span> <span class="type">int</span> major, <span class="type">const</span> <span class="type">char</span> *name, <span class="type">const</span> <span class="keyword">struct</span> file_operations *fops)</span>&#123;</span><br><span class="line"><span class="keyword">return</span> __register_chrdev(major, <span class="number">0</span>, <span class="number">256</span>, name, fops);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>

<ul>
<li>输入参数<ul>
<li>marjor：指定要申请的字符设备的主设备号，0表示自动分配一个未使用的主设备号</li>
<li>name：用于指定字符设备的名称</li>
<li>fops：文件操作寄存器</li>
</ul>
</li>
<li>返回值：主设备号</li>
</ul>
<h4 id="注销设备号"><a href="#注销设备号" class="headerlink" title="注销设备号"></a>注销设备号</h4><div class="highlight-container" data-rel="C"><figure class="iseeu highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">static</span> <span class="keyword">inline</span> <span class="type">void</span> <span class="title function_">unregister_chrdev</span><span class="params">(<span class="type">unsigned</span> <span class="type">int</span> major, <span class="type">const</span> <span class="type">char</span> *name)</span>&#123;</span><br><span class="line">  __unregister_chrdev(major, <span class="number">0</span>, <span class="number">256</span>, name);</span><br><span class="line">&#125;</span><br></pre></td></tr></table></figure></div>

<ul>
<li>输入参数<ul>
<li>marjor：指定要注销的字符设备的主设备号，0表示自动分配一个未使用的主设备号</li>
<li>name：用于指定字符设备的名称</li>
</ul>
</li>
<li>返回值：无</li>
</ul>
<h4 id="创建设备类"><a href="#创建设备类" class="headerlink" title="创建设备类"></a>创建设备类</h4><div class="highlight-container" data-rel="C"><figure class="iseeu highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">hello_class = class_create(THIS_MODULE, <span class="string">&quot;hello_class&quot;</span>);</span><br></pre></td></tr></table></figure></div>

<ul>
<li>输入参数<ul>
<li>THIS_MODULE</li>
<li>类名</li>
</ul>
</li>
<li>返回值：</li>
</ul>
<h4 id="注销设备类"><a href="#注销设备类" class="headerlink" title="注销设备类"></a>注销设备类</h4><div class="highlight-container" data-rel="C"><figure class="iseeu highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">class_destroy(hello_class);</span><br></pre></td></tr></table></figure></div>

<ul>
<li>输入参数<ul>
<li>clas_create 的输出</li>
</ul>
</li>
<li>返回值：无</li>
</ul>
<h4 id="设备节点的创建"><a href="#设备节点的创建" class="headerlink" title="设备节点的创建"></a>设备节点的创建</h4><div class="highlight-container" data-rel="C"><figure class="iseeu highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="keyword">struct</span> device *<span class="title function_">device_create</span><span class="params">(<span class="keyword">struct</span> class *class, <span class="keyword">struct</span> device *parent, <span class="type">dev_t</span> devt, <span class="type">void</span> *drvdata, <span class="type">const</span> <span class="type">char</span> *fmt, ...)</span></span><br></pre></td></tr></table></figure></div>

<ul>
<li>输入参数<ul>
<li>class：clas_create 的输出</li>
<li>parent：指向此设备的复结构设备指针，无就写NULL</li>
<li>devt：要添加的char设备，即主设备号下的子设备 <code>MKDEV(major,i)</code></li>
<li>drvdata：要添加到设备进行回调的数据</li>
<li>fmt：输入设备名称</li>
</ul>
</li>
<li>返回值：<ul>
<li>成功：返回struct device 结构体指针</li>
<li>失败：返回ERR_PTR()</li>
</ul>
</li>
</ul>
<h4 id="设备节点的删除"><a href="#设备节点的删除" class="headerlink" title="设备节点的删除"></a>设备节点的删除</h4><div class="highlight-container" data-rel="C"><figure class="iseeu highlight c"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line"><span class="type">void</span> <span class="title function_">device_destroy</span><span class="params">(<span class="keyword">struct</span> class *class, <span class="type">dev_t</span> devt)</span></span><br></pre></td></tr></table></figure></div>

<ul>
<li>输入参数<ul>
<li>class：clas_create 的输出</li>
<li>devt：要删除的cahr设备</li>
</ul>
</li>
<li>返回值：无</li>
</ul>
<h1 id="从设备节点资源获取相关–OF操作函数"><a href="#从设备节点资源获取相关–OF操作函数" class="headerlink" title="从设备节点资源获取相关–OF操作函数"></a>从设备节点资源获取相关–OF操作函数</h1><h4 id="根据节点路径寻找节点"><a href="#根据节点路径寻找节点" class="headerlink" title="根据节点路径寻找节点"></a>根据节点路径寻找节点</h4><div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">struct device_node *of_find_node_by_path(const char \*path)</span><br></pre></td></tr></table></figure></div>

<ul>
<li>输入参数<ul>
<li>path：节点在设备树中的路径</li>
</ul>
</li>
<li>返回值<ul>
<li>成功：device_node  结构体指针</li>
<li>失败：NULL</li>
</ul>
</li>
</ul>
<h4 id="根据节点名字寻找节点"><a href="#根据节点名字寻找节点" class="headerlink" title="根据节点名字寻找节点"></a>根据节点名字寻找节点</h4><div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">struct device_node *of_find_node_by_name(struct device_node \*from, const char \*name);</span><br></pre></td></tr></table></figure></div>

<ul>
<li>输入参数<ul>
<li>from：从哪个节点开始（根节点则设置为NULL）</li>
<li>name：需要查找的节点名</li>
</ul>
</li>
<li>返回值<ul>
<li>成功：device_node  结构体指针</li>
<li>失败：NULL</li>
</ul>
</li>
</ul>
<h4 id="寻找父节点"><a href="#寻找父节点" class="headerlink" title="寻找父节点"></a>寻找父节点</h4><div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">struct device_node *of_get_parent(const struct device_node \*node);</span><br></pre></td></tr></table></figure></div>

<ul>
<li>输入参数<ul>
<li>node：需要查找父节点的节点</li>
</ul>
</li>
<li>返回值<ul>
<li>成功：device_node  结构体指针</li>
<li>失败：NULL</li>
</ul>
</li>
</ul>
<h4 id="寻找子节点"><a href="#寻找子节点" class="headerlink" title="寻找子节点"></a>寻找子节点</h4><div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">struct device_node *of_get_next_child(const struct device_node \*node, struct device_node \*prev);</span><br></pre></td></tr></table></figure></div>

<ul>
<li>输入参数<ul>
<li>node：需要查找子节点的节点</li>
<li>prev：前一个子节点，寻找的是prev 节点之后的节点。NULL 表示寻找第一个子节点。</li>
</ul>
</li>
<li>返回值<ul>
<li>成功：device_node  结构体指针</li>
<li>失败：NULL</li>
</ul>
</li>
</ul>
<h1 id="设置GPIO状态"><a href="#设置GPIO状态" class="headerlink" title="设置GPIO状态"></a>设置GPIO状态</h1><h4 id="获取GPIO编号"><a href="#获取GPIO编号" class="headerlink" title="获取GPIO编号"></a>获取GPIO编号</h4><div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">static inline int of_get_named_gpio(struct device_node *np, const char *propname, int index)</span><br></pre></td></tr></table></figure></div>

<ul>
<li>输入参数<ul>
<li>np：指定设备节点</li>
<li>propname：GPIO属性名，与设备树中定义的属性名对应</li>
<li>index：引脚索引值，用于指定获取那个引脚</li>
</ul>
</li>
<li>返回值<ul>
<li>成功：获取到的GPIO编号</li>
<li>失败：负数</li>
</ul>
</li>
</ul>
<h4 id="申请GPIO"><a href="#申请GPIO" class="headerlink" title="申请GPIO"></a>申请GPIO</h4><div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">static inline int gpio_request(unsigned gpio, const char *label);</span><br></pre></td></tr></table></figure></div>

<ul>
<li>输入参数<ul>
<li>gpio：需要申请的GPIO，是 <code>of_get_named_gpio</code>的返回值。</li>
<li>label：引脚名字，相当于为申请得到的引脚取了个别名。</li>
</ul>
</li>
<li>返回值<ul>
<li>成功：返回0</li>
<li>失败：负数</li>
</ul>
</li>
</ul>
<h4 id="释放GPIO"><a href="#释放GPIO" class="headerlink" title="释放GPIO"></a>释放GPIO</h4><div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">static inline void gpio_free(unsigned gpio);</span><br></pre></td></tr></table></figure></div>

<ul>
<li>输入参数<ul>
<li>gpio：需要释放的GPIO，是 <code>of_get_named_gpio</code>的返回值。</li>
</ul>
</li>
<li>无返回值</li>
</ul>
<h4 id="设置GPIO输出函数：将引脚设置为输出模式"><a href="#设置GPIO输出函数：将引脚设置为输出模式" class="headerlink" title="设置GPIO输出函数：将引脚设置为输出模式"></a>设置GPIO输出函数：将引脚设置为输出模式</h4><div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">static inline int gpio_direction_output(unsigned gpio , int value);</span><br></pre></td></tr></table></figure></div>

<ul>
<li>输入参数<ul>
<li>gpio：需要设置的GPIO，是 <code>of_get_named_gpio</code>的返回值。</li>
<li>value：输出值，1-高电平，0-低电平。</li>
</ul>
</li>
<li>返回值<ul>
<li>成功：返回0</li>
<li>失败：负数</li>
</ul>
</li>
</ul>
<h4 id="设置GPIO输入函数：将引脚设置为输入模式"><a href="#设置GPIO输入函数：将引脚设置为输入模式" class="headerlink" title="设置GPIO输入函数：将引脚设置为输入模式"></a>设置GPIO输入函数：将引脚设置为输入模式</h4><div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">static inline int gpio_direction_input(unsigned gpio);</span><br></pre></td></tr></table></figure></div>

<ul>
<li>输入参数<ul>
<li>gpio：需要设置的GPIO，是 <code>of_get_named_gpio</code>的返回值。</li>
</ul>
</li>
<li>返回值<ul>
<li>成功：返回0</li>
<li>失败：负数</li>
</ul>
</li>
</ul>
<h4 id="获取GPIO引脚值：无论引脚为输入模式or输出模式都可以使用"><a href="#获取GPIO引脚值：无论引脚为输入模式or输出模式都可以使用" class="headerlink" title="获取GPIO引脚值：无论引脚为输入模式or输出模式都可以使用"></a>获取GPIO引脚值：无论引脚为输入模式or输出模式都可以使用</h4><div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">static inline int gpio_get_value(unsigned gpio);</span><br></pre></td></tr></table></figure></div>

<ul>
<li>输入参数<ul>
<li>gpio：需要获取值的GPIO，是 <code>of_get_named_gpio</code>的返回值。</li>
</ul>
</li>
<li>返回值<ul>
<li>成功：返回引脚状态</li>
<li>失败：负数</li>
</ul>
</li>
</ul>
<h4 id="设置GPIO引脚输出值：只适用于引脚为输出模式"><a href="#设置GPIO引脚输出值：只适用于引脚为输出模式" class="headerlink" title="设置GPIO引脚输出值：只适用于引脚为输出模式"></a>设置GPIO引脚输出值：只适用于引脚为输出模式</h4><div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">static inline int gpio_set_value(unsigned gpio, int value);</span><br></pre></td></tr></table></figure></div>

<ul>
<li>输入参数<ul>
<li>gpio：需要设置值的GPIO，是 <code>of_get_named_gpio</code>的返回值。</li>
<li>value：输出值，1-高电平，0-低电平。</li>
</ul>
</li>
<li>返回值<ul>
<li>成功：返回0</li>
<li>失败：负数</li>
</ul>
</li>
</ul>
<h1 id="中断相关"><a href="#中断相关" class="headerlink" title="中断相关"></a>中断相关</h1><h4 id="解析并映射中断函数"><a href="#解析并映射中断函数" class="headerlink" title="解析并映射中断函数"></a>解析并映射中断函数</h4><div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">unsigned int irq_of_parse_and_map(struct device_node *dev, int index);</span><br></pre></td></tr></table></figure></div>

<ul>
<li>从设备树中获取中断，并将中断ID转化为linux内核虚拟IRQ number。</li>
<li>输入参数<ul>
<li>dev：用于指定设备节点</li>
<li>index：指定解析、映射第几个中断，从0开始</li>
</ul>
</li>
<li>返回值<ul>
<li>成功：解析、映射得到的内核中断号</li>
<li>失败：0</li>
</ul>
</li>
</ul>
<h4 id="中断注册函数"><a href="#中断注册函数" class="headerlink" title="中断注册函数"></a>中断注册函数</h4><div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">static inline int __must_check request_irq(unsigned int irq, irq_handler_t handler, unsigned long flags, const char *name, void *dev);</span><br></pre></td></tr></table></figure></div>

<ul>
<li>输入参数<ul>
<li>irq：用于指定内核中断号，从设备树中得到；</li>
<li>handler：用于指定中断服务函数</li>
<li>flags：触发条件，会覆盖设备树中的配置</li>
<li>name：中断的名字，在 <code>/proc/interrupts</code>目录下看到对应的文件。</li>
<li>dev：</li>
</ul>
</li>
<li>返回值<ul>
<li>成功：返回0</li>
<li>失败：负数</li>
</ul>
</li>
</ul>
<h4 id="中断注销函数"><a href="#中断注销函数" class="headerlink" title="中断注销函数"></a>中断注销函数</h4><div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">void free_irq(unsigned int irq, void *dev);</span><br></pre></td></tr></table></figure></div>

<ul>
<li>输入参数<ul>
<li>irq：从设备树中得到或者转换得到的中断编号</li>
<li>dev：</li>
</ul>
</li>
<li>无返回值</li>
</ul>
<h4 id="中断处理函数"><a href="#中断处理函数" class="headerlink" title="中断处理函数"></a>中断处理函数</h4><div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">irqreturn_t (*irq_handler_t)(int irq, void * dev);</span><br></pre></td></tr></table></figure></div>

<ul>
<li>输入参数<ul>
<li>irq：从设备树中得到或者转换得到的中断编号</li>
<li>dev：</li>
</ul>
</li>
<li>返回值<ul>
<li>rqreturn_t：枚举类型变量</li>
</ul>
</li>
</ul>
<h4 id="中断的使能与失能"><a href="#中断的使能与失能" class="headerlink" title="中断的使能与失能"></a>中断的使能与失能</h4><div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">void enable_irq(unsigned int irq)</span><br><span class="line">void disable_irq(unsigned int irq)</span><br></pre></td></tr></table></figure></div>

<h1 id="原子操作"><a href="#原子操作" class="headerlink" title="原子操作"></a>原子操作</h1><h4 id="定义整型原子变量并设置初始值"><a href="#定义整型原子变量并设置初始值" class="headerlink" title="定义整型原子变量并设置初始值"></a>定义整型原子变量并设置初始值</h4><div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br><span class="line">3</span><br><span class="line">4</span><br><span class="line">5</span><br><span class="line">6</span><br><span class="line">7</span><br><span class="line">8</span><br></pre></td><td class="code"><pre><span class="line">//atomic_t 类型结构体</span><br><span class="line">typedef struct &#123;</span><br><span class="line">int counter;</span><br><span class="line">&#125; atomic_t;</span><br><span class="line"></span><br><span class="line"></span><br><span class="line">#define ATOMIC_INIT(i)          &#123; (i) &#125;</span><br><span class="line">atomic_t data = ATOMIC_INIT(int i);</span><br></pre></td></tr></table></figure></div>

<ul>
<li>输入参数<ul>
<li>i：设置整型原子变量的初始值</li>
<li>atomic_t：整型原子类型</li>
</ul>
</li>
</ul>
<h4 id="设置整型原子变量的值"><a href="#设置整型原子变量的值" class="headerlink" title="设置整型原子变量的值"></a>设置整型原子变量的值</h4><div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br></pre></td><td class="code"><pre><span class="line">#define atomic_set(v, i)   WRITE_ONCE(((v)-&gt;counter), (i))</span><br></pre></td></tr></table></figure></div>

<ul>
<li>输入参数<ul>
<li>v：要设置的整型原子变量的指针（地址）</li>
<li>i：设置的值</li>
</ul>
</li>
</ul>
<h4 id="获取整型原子变量的值"><a href="#获取整型原子变量的值" class="headerlink" title="获取整型原子变量的值"></a>获取整型原子变量的值</h4><div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">#define atomic_read(v)       ACCESS_ONCE((v)-&gt;counter)</span><br><span class="line">atomic_read(atomic_t* v)</span><br></pre></td></tr></table></figure></div>

<ul>
<li>输入参数<ul>
<li>v：要获取的整型原子变量的指针（地址）</li>
</ul>
</li>
<li>返回值：获取到的整型原子变量的值</li>
</ul>
<h4 id="整型原子变量自增-自减"><a href="#整型原子变量自增-自减" class="headerlink" title="整型原子变量自增&#x2F;自减"></a>整型原子变量自增&#x2F;自减</h4><div class="highlight-container" data-rel="Plaintext"><figure class="iseeu highlight plaintext"><table><tr><td class="gutter"><pre><span class="line">1</span><br><span class="line">2</span><br></pre></td><td class="code"><pre><span class="line">void atomic_inc(atomic_t *v); //整型原子变量自增1</span><br><span class="line">void atomic_dec(atomic_t *v);//整型原子变量自减1</span><br></pre></td></tr></table></figure></div>

<ul>
<li>输入参数<ul>
<li>v：要获取的整型原子变量的指针（地址）</li>
</ul>
</li>
</ul>

        </div>

        

        

        

        
            <div class="article-nav">
                
                    <div class="article-prev">
                        <a class="prev"
                        rel="prev"
                        href="/blog/2023/09/01/Linux%E7%9B%B8%E5%85%B3/"
                        >
                            <span class="left arrow-icon flex-center">
                                <i class="fa-solid fa-chevron-left"></i>
                            </span>
                            <span class="title flex-center">
                                <span class="post-nav-title-item">Linux相关</span>
                                <span class="post-nav-item">Prev posts</span>
                            </span>
                        </a>
                    </div>
                
                
                    <div class="article-next">
                        <a class="next"
                        rel="next"
                        href="/blog/2023/08/27/Linux_Driver/%E9%A9%B1%E5%8A%A8%E4%BB%A3%E7%A0%81/"
                        >
                            <span class="title flex-center">
                                <span class="post-nav-title-item">驱动代码</span>
                                <span class="post-nav-item">Next posts</span>
                            </span>
                            <span class="right arrow-icon flex-center">
                                <i class="fa-solid fa-chevron-right"></i>
                            </span>
                        </a>
                    </div>
                
            </div>
        


        
            <div class="comment-container">
                <div class="comments-container pjax">
    <div id="comment-anchor"></div>
    <div class="comment-area-title">
        <i class="fa-solid fa-comments"></i>&nbsp;Comments
    </div>
    

        
            
 
    <div id="waline"></div>
    <script type="module"  data-swup-reload-script>
        import { init } from 'https://evan.beee.top/js/waline.mjs';

        function loadWaline() {
            init({
                el: '#waline',
                serverURL: 'https://example.example.com',
                lang: 'zh-CN',
                dark: 'body[class~="dark-mode"]',
                requiredMeta: ['nick','mail'], // cannot customize by theme config, change it yourself
            });
        }

        if ('true') {
            const loadWalineTimeout = setTimeout(() => {
                loadWaline();
                clearTimeout(loadWalineTimeout);
            }, 1000);
        } else {
            window.addEventListener('DOMContentLoaded', loadWaline);
        }
        
    </script>



        
    
</div>

            </div>
        
    </div>

    
        <div class="toc-content-container">
            <div class="post-toc-wrap">
    <div class="post-toc">
        <div class="toc-title">On this page</div>
        <div class="page-title">Linux驱动函数介绍</div>
        <ol class="nav"><li class="nav-item nav-level-1"><a class="nav-link" href="#%E6%B3%A8%E5%86%8C%E5%87%BD%E6%95%B0"><span class="nav-text">注册函数</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%BB%8E%E8%AE%BE%E5%A4%87%E8%8A%82%E7%82%B9%E8%B5%84%E6%BA%90%E8%8E%B7%E5%8F%96%E7%9B%B8%E5%85%B3%E2%80%93OF%E6%93%8D%E4%BD%9C%E5%87%BD%E6%95%B0"><span class="nav-text">从设备节点资源获取相关–OF操作函数</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E8%AE%BE%E7%BD%AEGPIO%E7%8A%B6%E6%80%81"><span class="nav-text">设置GPIO状态</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E4%B8%AD%E6%96%AD%E7%9B%B8%E5%85%B3"><span class="nav-text">中断相关</span></a></li><li class="nav-item nav-level-1"><a class="nav-link" href="#%E5%8E%9F%E5%AD%90%E6%93%8D%E4%BD%9C"><span class="nav-text">原子操作</span></a></li></ol>

    </div>
</div>
        </div>
    
</div>



                

            </div>
            
            

        </div>

        <div class="main-content-footer">
            <footer class="footer">
    <div class="info-container">
        <div class="copyright-info">
            &copy;
            
              <span>2022</span>
              -
            
            2024&nbsp;&nbsp;<i class="fa-solid fa-heart fa-beat" style="--fa-animation-duration: 0.5s; color: #f54545"></i>&nbsp;&nbsp;<a href="/">ChenGreen</a>
        </div>
        
            <script data-swup-reload-script src="https://busuanzi.ibruce.info/busuanzi/2.3/busuanzi.pure.mini.js"></script>
            <div class="website-count info-item">
                
                    <span id="busuanzi_container_site_uv" class="busuanzi_container_site_uv">
                        VISITOR COUNT&nbsp;<span id="busuanzi_value_site_uv" class="busuanzi_value_site_uv"></span>
                    </span>
                
                
                    <span id="busuanzi_container_site_pv" class="busuanzi_container_site_pv">
                        TOTAL PAGE VIEWS&nbsp;<span id="busuanzi_value_site_pv" class="busuanzi_value_site_pv"></span>
                    </span>
                
            </div>
        
        <div class="theme-info info-item">
            <span class="powered-by-container">POWERED BY <?xml version="1.0" encoding="utf-8"?><!DOCTYPE svg PUBLIC "-//W3C//DTD SVG 1.1//EN" "http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd"><svg version="1.1" id="圖層_1" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" x="0px" y="0px" width="1rem" height="1rem" viewBox="0 0 512 512" enable-background="new 0 0 512 512" xml:space="preserve"><path fill="#0E83CD" d="M256.4,25.8l-200,115.5L56,371.5l199.6,114.7l200-115.5l0.4-230.2L256.4,25.8z M349,354.6l-18.4,10.7l-18.6-11V275H200v79.6l-18.4,10.7l-18.6-11v-197l18.5-10.6l18.5,10.8V237h112v-79.6l18.5-10.6l18.5,10.8V354.6z"/></svg><a target="_blank" href="https://hexo.io">Hexo</a></span>
                <br>
            <span class="theme-version-container">THEME&nbsp;<a class="theme-version" target="_blank" href="https://github.com/EvanNotFound/hexo-theme-redefine">Redefine v2.3.0</a></span>
        </div>
        
        
        
        
        
        
    </div>  
</footer>
        </div>
    </div>

    
        <div class="post-tools">
            <div class="post-tools-container">
    <ul class="article-tools-list">
        <!-- TOC aside toggle -->
        
            <li class="right-bottom-tools page-aside-toggle">
                <i class="fa-regular fa-outdent"></i>
            </li>
        

        <!-- go comment -->
        
            <li class="go-comment">
                <i class="fa-regular fa-comments"></i>
            </li>
        
    </ul>
</div>

        </div>
    

    <div class="right-side-tools-container">
        <div class="side-tools-container">
    <ul class="hidden-tools-list">
        <li class="right-bottom-tools tool-font-adjust-plus flex-center">
            <i class="fa-regular fa-magnifying-glass-plus"></i>
        </li>

        <li class="right-bottom-tools tool-font-adjust-minus flex-center">
            <i class="fa-regular fa-magnifying-glass-minus"></i>
        </li>

        <li class="right-bottom-tools tool-expand-width flex-center">
            <i class="fa-regular fa-expand"></i>
        </li>

        <li class="right-bottom-tools tool-dark-light-toggle flex-center">
            <i class="fa-regular fa-moon"></i>
        </li>

        <!-- rss -->
        

        

        <li class="right-bottom-tools tool-scroll-to-bottom flex-center">
            <i class="fa-regular fa-arrow-down"></i>
        </li>
    </ul>

    <ul class="visible-tools-list">
        <li class="right-bottom-tools toggle-tools-list flex-center">
            <i class="fa-regular fa-cog fa-spin"></i>
        </li>
        
            <li class="right-bottom-tools tool-scroll-to-top flex-center">
                <i class="arrow-up fas fa-arrow-up"></i>
                <span class="percent"></span>
            </li>
        
        
    </ul>
</div>

    </div>

    <div class="image-viewer-container">
    <img src="">
</div>


    


</main>


<script src="/blog/js/libs/Swup.min.js"></script>

<script src="/blog/js/libs/SwupSlideTheme.min.js"></script>

<script src="/blog/js/libs/SwupScriptsPlugin.min.js"></script>

<script src="/blog/js/libs/SwupProgressPlugin.min.js"></script>

<script src="/blog/js/libs/SwupScrollPlugin.min.js"></script>

<script>
    const swup = new Swup({
        plugins: [
            new SwupScriptsPlugin({
                optin: true,
            }),
            new SwupProgressPlugin(),
            new SwupScrollPlugin({
                offset: 80,
            }),
            new SwupSlideTheme({
                mainElement: ".main-content-body",
            }),
        ],
        containers: ["#swup"],
    });

    swup.on("pageView", () => {
        Global.refresh();
    });

    // if (document.readyState === "complete") {
    //
    // } else {
    //     document.addEventListener("DOMContentLoaded", () => init());
    // }
</script>





<script src="/blog/js/utils.js"></script>

<script src="/blog/js/main.js"></script>

<script src="/blog/js/layouts/navbarShrink.js"></script>

<script src="/blog/js/tools/scrollTopBottom.js"></script>

<script src="/blog/js/tools/lightDarkSwitch.js"></script>

<script src="/blog/js/layouts/categoryList.js"></script>





    
<script src="/blog/js/tools/codeBlock.js"></script>




    
<script src="/blog/js/layouts/lazyload.js"></script>






  
<script src="/blog/js/libs/Typed.min.js"></script>

  
<script src="/blog/js/plugins/typed.js"></script>







<div class="post-scripts" data-swup-reload-script>
    
        
<script src="/blog/js/tools/tocToggle.js"></script>

<script src="/blog/js/layouts/toc.js"></script>

<script src="/blog/js/libs/anime.min.js"></script>

<script src="/blog/js/plugins/tabs.js"></script>

    
</div>


</body>
</html>
